package net.siufung.market.base.app.controller.regulation;

import com.google.common.base.Strings;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.app.controller.model.app.AppSaveDto;
import net.siufung.market.base.app.controller.model.app.AppUpdateDto;
import net.siufung.market.base.app.mapper.IAppMapper;
import net.siufung.market.base.app.spi.ICheckMenuOccupySpi;

import java.util.Objects;

/**
 * @author 陈建峰
 * @date 2023/8/3 1:31 PM
 * @since V1.0.0
 */
public class AppRegulation {

    static VerifyRepeatDto<Integer> appVerifyRepeatDto = new VerifyRepeatDto<>();

    @Slf4j
    @RequiredArgsConstructor
    public static class Save extends AbstractRegulation<AppSaveDto> {

        private static final long serialVersionUID = -6144004287767301772L;

        private final IAppMapper appMapper;

        @Override
        public boolean isSatisfiedBy(AppSaveDto appSaveDto) {
            appVerifyRepeatDto.setMethodEventEnum(MethodEventEnum.ADD);
            appVerifyRepeatDto.setPk(null);
            appVerifyRepeatDto.setVerifyKeyValue(appSaveDto.getAppName());
            if(!appMapper.checkAppNameNotRepeat(appVerifyRepeatDto)){
                throw new BizServiceException("应用名称重复！");
            }
            if(Objects.nonNull(appSaveDto.getAppCode())){
                appVerifyRepeatDto.setVerifyKeyValue(appSaveDto.getAppCode());
                if(!appMapper.checkAppCodeNotRepeat(appVerifyRepeatDto)){
                    throw new BizServiceException("应用编码重复！");
                }
            }
            appVerifyRepeatDto.setVerifyKeyValue(appSaveDto.getClientId());
            if(!appMapper.checkClientIdNotRepeat(appVerifyRepeatDto)){
                throw new BizServiceException("应用客户端ID重复！");
            }
            return Boolean.TRUE;
        }
    }

    @Slf4j
    @RequiredArgsConstructor
    public static class Update extends AbstractRegulation<AppUpdateDto> {

        private static final long serialVersionUID = -3446267225664888067L;

        private final IAppMapper appMapper;

        @Override
        public boolean isSatisfiedBy(AppUpdateDto appUpdateDto) {
            appVerifyRepeatDto.setMethodEventEnum(MethodEventEnum.UPDATE);
            appVerifyRepeatDto.setPk(appUpdateDto.getId());
            appVerifyRepeatDto.setVerifyKeyValue(appUpdateDto.getAppName());
            if (!appMapper.checkAppNameNotRepeat(appVerifyRepeatDto)) {
                throw new BizServiceException("应用名称重复！");
            }
            if (!Strings.isNullOrEmpty(appUpdateDto.getAppCode())) {
                appVerifyRepeatDto.setVerifyKeyValue(appUpdateDto.getAppCode());
                if (!appMapper.checkAppCodeNotRepeat(appVerifyRepeatDto)) {
                    throw new BizServiceException("应用编码重复！");
                }
            }
            appVerifyRepeatDto.setVerifyKeyValue(appUpdateDto.getClientId());
            if (!appMapper.checkClientIdNotRepeat(appVerifyRepeatDto)) {
                throw new BizServiceException("应用客户端ID重复！");
            }
            return Boolean.TRUE;
        }
    }


    @Slf4j
    @RequiredArgsConstructor
    public static class Remove extends AbstractRegulation<Integer> {

        private static final long serialVersionUID = 3923893568821655485L;

        private final ICheckMenuOccupySpi checkMenuOccupySpi;

        @Override
        public boolean isSatisfiedBy(Integer id) {
            if (!checkMenuOccupySpi.allowRemove(id)) {
                throw new BizServiceException("应用被占用，不允许删除！");
            }
            return Boolean.TRUE;
        }
    }

    @Slf4j
    @RequiredArgsConstructor
    public static class Status extends AbstractRegulation<Integer> {

        private static final long serialVersionUID = 2266461877054163300L;

        private final ICheckMenuOccupySpi checkMenuOccupySpi;

        @Override
        public boolean isSatisfiedBy(Integer id) {
            if(!checkMenuOccupySpi.allowStatus(id)){
                throw new BizServiceException("应用被占用，不允许变更状态！");
            }
            return Boolean.TRUE;
        }
    }
}
